<?php
/**
 * Ruth Library
 * $Id: Options.php 2 2008-01-17 02:59:56Z baron314159@yahoo.com $
 *
 * LICENSE
 *
 * Copyright (c) 2007, Chris Utz
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Impact Lab nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY CHRIS UTZ ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL CHRIS UTZ BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @category   Ruth
 * @package    Ruth_Options
 * @author     Chris Utz <chris@chrisutz.com>
 * @copyright  2007 Chris Utz <chris@chrisutz.com>
 * @license    http://www.opensource.org/licenses/bsd-license.php    New BSD License
 * @version    $Revision: 2 $
 */

/**
 * @see Ruth_Options_Exception
 */
require_once 'Ruth/Options/Exception.php';

/**
 * Holds options used by Validate and Filter classes.
 *
 * @category   Ruth
 * @package    Ruth_Options
 * @author     Chris Utz <chris@chrisutz.com>
 * @copyright  2007 Chris Utz <chris@chrisutz.com>
 * @license    http://www.opensource.org/licenses/bsd-license.php    New BSD License
 * @version    $Revision: 2 $
 */
class Ruth_Options
{
    /**
     * The option data.
     *
     * @var array
     */
    protected $_data = array();

    /**
     * Creates a Ruth_Options instance using given param to populate the initial options.
     *
     * @param array|null $param Initial option data.
     * @return void
     * @throws Ruth_Options_Exception
     */
    public function __construct($param=null)
    {
        if (is_array($param)) {
            $this->setOptions($param);
        } else if ($param !== null) {
            throw new Ruth_Options_Exception("parameter must be an array or null");
        }
    }

    /**
     * Merges options in parameter array into internal options array, handling 
     * pairs of numeric entries as associative entries. Ensures set options are
     * strings.
     *
     * @param array $options The options to merge
     * @throws Ruth_Options_Exception
     */
    public function setOptions(array $options)
    {
        $numIdxKey = null;

        foreach ($options as $key => $value) {
            if (is_string($key)) {
                $this->_data[$key] = $value;
            } else if (is_int($key)) {
                if (is_null($numIdxKey)) {
                    $numIdxKey = $value;
                } else {
                    $this->_data[$numIdxKey] = $value;
                    $numIdxKey = null;
                }
            }
        }

        if (!is_null($numIdxKey)) {
            throw new Ruth_Options_Exception("option key missing matching value");
        }
    }

    /**
     * Unsets all options, or just the one specified by the $name parameter.
     *
     * @param null|string $name The option to unset, or null to unset all options.
     */
    public function clear($name=null)
    {
        if (is_null($name)) {
            $this->_data = array();
        } else {
            unset($this->_data[$name]);
        }
    }

    /**
     * Returns the option data as an array.
     *
     * @return array 
     */
    public function getData()
    {
        return $this->_data;
    }

    /**
     * Gets an option value or returns $default if the option specified by $name
     * does not exist.
     *
     * @param string $name
     * @param mixed $default
     * @return mixed The value of the option or $default
     */
    public function get($name, $default=null)
    {
        if (array_key_exists($name, $this->_data)) {
            return $this->_data[$name];
        } else {
            return $default;
        }
    }

    /**
     * Getter magic method.
     *
     * @param string $name The option to fetch.
     * @return string The value of the option or null if it does not exist.
     */
    public function __get($name)
    {
        return $this->get($name);
    }

    /**
     * isset() magic method.
     *
     * @param string $name The option to check.
     * @return boolean
     */
    public function __isset($name)
    {
        return isset($this->_data[$name]);
    }

    /**
     * Setter magic method.
     *
     * @param string $name The option name to set
     * @param string $value The value to set
     * @throws Ruth_Options_Exception
     */
    public function __set($name, $value)
    {
        $this->_data[$name] = $value;
    }

    /**
     * unset() magic method.
     *
     * @param string $name The option to unset.
     */
    public function __unset($name)
    {
        unset($this->_data[$name]);
    }
}

// vim: sw=4:ts=4:sts=4:et
